#!/usr/bin/perl 

use strict;

use DBI;
use CGI;
# use TTrakerSite;
use TTraker;

my $dbh;
& initialize_dbi;

my $form_ref = &parse_input;
& print_header;

my %FORM_DATA = %$form_ref; 

my $num = $FORM_DATA{'supNum'};
my $name = $FORM_DATA{'supName'};
my $job_id = $FORM_DATA{'supJob'};
my $loc = $FORM_DATA{'supLoc'};
my $activity = $FORM_DATA{'activity'};

my $week = get_current_week;

# print "NUM: $num NAME: $name ACTIVITY: $activity<br/><br/>";

my ($now, $now_date, $now_time, $logged_in, $stmnt);

# print "activity is $activity<br/>";

if ($activity == 1 ) {
#    & confirm_payroll_hours;
    select_payroll_dates($num, $name, $job_id, $loc); 
} elsif ($activity == 2 ) {
#    & check_pay_week (0);
#   & finish_job;
} elsif ($activity == 3 ) {
#    & check_pay_week (-1);
#   & log_tasks;
} elsif ($activity == 5 ) {	
	add_new_temp($num, $name, $job_id, $loc, $activity);
} elsif ($activity == 5 ) {
#   & lunch_time;
} elsif ($activity == 6 ) {
   who_is_logged_in ($num, $name, $job_id, $loc, $activity);
} elsif ($activity == 7 ) {
   time_card_specs ($num,$name,$job_id,$loc);
} elsif ($activity == 8 ) {
	employee_job_task_specs ($num,$name,$job_id,$loc);
} elsif ($activity == 9 ) {
   # no activity
} elsif ($activity > 100 ) {
    & review_payroll_week ($num, $name, $job_id, $loc, $activity);	
} else {
#   & return_to_activity
}

db_disconnect();

############### SUBROUTINES FOLLOW ###############

sub employee_job_task_specs 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @payDates=();		
	my $payDates_ref = get_payroll_dates_incl_today;
   @payDates = @$payDates_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<h3>Modify Tasks for Employee</h3>
	<div id='box1'>
	Supervisor Number:<br/><br/>
	Supervisor Name:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_sup_task_job_data.pl' method='post'>
	<input type="hidden" name=supNum value='$num'/>
	<input type="hidden" name=supName value='$name'/>
	<input type="hidden" name=supJob value='$job'/>
	<input type="hidden" name=supLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<table cellspacing = "4" cellpadding="6">
		<tr>
			<td>Employee Number: &nbsp</td>
			<td><input type="text" size=5 name="empNum" /></td>
		</tr>
		<tr>
			<td>Select Date:</td>
			<td>
				<select style="width:120px" id=payDate name="payDate" tabindex=1 >
ENDHEADER
			foreach $date (@payDates) {
   			print "<option value=$date >$date</option>";
			}
print <<ENDHEADER;
			</td>
	</table>
<br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_03_get_employee_list.pl?loc=$loc" target="_blank">Get Employee List<a/>
<br/><br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
</form>
</body>
</html>

ENDHEADER

}


sub time_card_specs 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @payDates=();		
	my $payDates_ref = get_payroll_dates;
   @payDates = @$payDates_ref;
	my @facility=(); 
   my $facility_ref = get_facilities();
	my @facility = @$facility_ref;
	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id='clock'></div>
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_sup_time_card_data.pl' method='post'>
	<input type="hidden" name=supNum value='$num'/>
	<input type="hidden" name=supName value='$name'/>
	<input type="hidden" name=supJob value='$job'/>
	<input type="hidden" name=supLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<table cellspacing = "4" cellpadding="6">
		<tr>
			<td>Employee Number: &nbsp</td>
			<td><input type="text" size=5 name="empNum" /></td>
		</tr>
		<tr>
			<td>Select Date:</td>
			<td>
				<select style="width:120px" id=payDate name="payDate" tabindex=1 >
ENDHEADER
			foreach $date (@payDates) {
   			print "<option value=$date >$date</option>";
			}
print <<ENDHEADER;
			</td>
		</tr>
		</select>	
		<tr>
			<td>Location: &nbsp</td>
			<td><select style="width:120px" id=empFac name="empFac" tabindex=1 >
ENDHEADER
			# add default option
			print "<option value=0 >Home</option>";
			for (my $i=0;$i<=$#facility;$i++) {
				print "<option value=$facility[$i][0] >$facility[$i][1]</option>";
			}
			print "<option value=99 >All</option>";
			
			print <<ENDHEADER;
			</td>
		</tr>
		</select>
	</table>
<br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<a href="${cgi_ref}trak_03_get_employee_list.pl?loc=$loc" target="_blank">Get Employee List<a/>
<br/><br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
</form>
</body>
</html>

ENDHEADER

}

sub who_is_logged_in
{
my $num = $_[0];
my $name = $_[1];
my $job = $_[2];
my $loc = $_[3];
my $activity = $_[4];

my $work_list_ref = get_current_work_list($loc);
my @work_list = @$work_list_ref;

print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>who is logged in?</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1"> 
  <h3>Currently Logged In<br/><br/></h3>
<table border = "0" cellspacing = "6" cellpadding = "2">
<tr>
	<th align="left">#</th>
   <th align="left">Name</th>
   <th align="left">Day Start</th>
   <th align="left">Doc #</th>
   <th align="left">Client</th>
   <th align="left">Job Start</th>
</tr>
ENDHEADER

for my $i (0..$#work_list ) {
   my $num = $work_list[$i][0];
   my $name = $work_list[$i][1];
   my $day_start = $work_list[$i][2];
	my $doc = $work_list[$i][3];
	my $cli = $work_list[$i][4];
	my $job_start = $work_list[$i][5];

   print <<ENDHEADER;
   <tr border ="0">
     <td>$num &nbsp&nbsp</td>
     <td>$name &nbsp&nbsp</td>
     <td>$day_start &nbsp&nbsp</td>
     <td>$doc &nbsp&nbsp</td>
     <td>$cli &nbsp&nbsp</td>
     <td>$job_start &nbsp&nbsp</td>
   </tr>
ENDHEADER
}
print <<ENDHEADER;
</table>
<br/>
<a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
</div>
</body>
</html>
ENDHEADER

}

sub add_new_temp
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];	
	my $activity = $_[4];
print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_02_sup_activity_select_add_new_temp</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  &nbsp&nbsp&nbsp&nbsp&nbsp
  <div id = "box5_1"> 
  <h3>Add New Temp Employee</h3>
  </div>
ENDHEADER

#<div id="box1">
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp First Name: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Last Name: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Login ID: <br/><br/>
#  &nbsp&nbsp&nbsp&nbsp&nbsp&nbsp&nbsp Agency: <br/><br/>
# </div>
print <<ENDHEADER;

<form id="box2" autocomplete="off" name="empID" action="${cgi_ref}trak_03_add_new_temp.pl" method="post">
	<input type="hidden" name=empNum value='$num'/>
	<input type="hidden" name=empName value='$name'/>
	<input type="hidden" name=empJob value='$job'/>
	<input type="hidden" name=empLoc value='$loc'/>
	<table>
		<tr>
			<td>First Name: &nbsp</td>
			<td><input type="text" size=24 name=tmpFirst /></td>
		</tr>
		<tr>
			<td>Last Name: </td>
			<td><input type="text" size=24 name=tmpLast /></td>
		</tr>
		<tr>
		   <td>Login ID: </td>
		   <td><input type="text" size=10 name=tmpLogin /></td>
		</tr>
		<tr>
			<td>Location: </td>
			<td><label id=info ><input type="text" size=5 name=tmpLocation />&nbsp VAN or TOR</label></td>
		</tr>
		<tr>
   		<td>Facility #:</td>
   		<td><input type="text" size=5 name=tmpFacility /></td>
   	<tr/>
   	<tr>
	   	<td>Agency: </td>
	   	<td><input type="text" size=5 name=tmpAgency /></td>
		</tr>
		<tr>
   		<td>Rate: </td>
   		<td><input type="text" size=5 name=tmpRate /><td>
		</tr>
	</table>
	<br/>
   <input type="submit" value="Add New Temp">
   <br/><br/>
	<a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a>
 	</form>
 	</div>
 	</body>
 	</html>
ENDHEADER

}

sub select_payroll_dates 
{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $date;
 	my @payDates=();		
	my $payDates_ref = get_payroll_dates;
    @payDates = @$payDates_ref;

	print <<ENDHEADER;
	<html>
	<head>
	<link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
	<script type='text/javascript' src='${js_ref}trak.js'></script>
	<title>trak_02_sup_activity_select_payroll_date</title>
	</head>
	<body onload='startUp1()'>
	<h2><center>${banner}</center></h2>
	<hr/>
	<div id='clock'></div>
	<div id='box1'>
	Employee Number:<br/><br/>
	Employee Name:<br/><br/>
	Select Date:<br/><br/>
	</div>
	<form id='box2' autocomplete='off' name='startJob' action='${cgi_ref}trak_03_sup_confirm_payroll.pl' method='post'>
	<input type="hidden" name=supNum value='$num'/>
	<input type="hidden" name=supName value='$name'/>
	<input type="hidden" name=supJob value='$job'/>
	<input type="hidden" name=supLoc value='$loc'/>
	<span>$num</span><br/><br/>
	<span>$name</span></b><br/><br/>
	<select id=payDate name="payDate" tabindex=1 >
ENDHEADER
foreach $date (@payDates) {
   print "<option value=$date >$date</option>";
}
print <<ENDHEADER;
</select>
<br/><br/>
<input type='submit' name='submit' value='Proceed'>
<br/><br/>
<p><a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a></p>
</form>
</body>
</html>

ENDHEADER

}


sub review_payroll_week	

{
	my $num = $_[0];
	my $name = $_[1];
	my $job = $_[2];
	my $loc = $_[3];
	my $activity = $_[4];
   	my $payNum_ref = get_payroll_week_of_interest ($activity);
	my @woi = @$payNum_ref;	
	my $start = $woi[0];
	my $end = $woi[1];
	my $payNum = $woi[2];
   	my $week_str = "$start - $end";
	my $weekPay_ref = get_payroll_weekly_list ($activity, $loc);
	my @pay = @$weekPay_ref;
#	print "got start: $start, end: $end, num: $payNum  THERE ARE: $#pay ENTRIES<br/>"; 
	
#	for $i (0..$#pay ) {
#		print "count $i ";
#			for ($j=0;$j<20;$j++) { 
#	 		print "$pay[$i][$j], ";
#	 	}
#	 	print "<br/>";
#	 }	
  print <<ENDHEADER;
  <html>
  <head>
  <link rel='stylesheet' type='text/css' href='${css_ref}trak.css' />
  <script type='text/javascript' src='${js_ref}trak.js'></script>
  <title>trak_03_sup_confirm_payroll.pl</title>
  </head>
  <body>
  <h2><center>${banner}</center></h2>
  <hr/>
  <div id='box5_1'>
  <h3>Payroll Hours for Week of $week_str</h3>
  <form onkeypress='return event.keyCode != 13;' autocomplete='off'  name='activity' action='${cgi_ref}trak_03_sup_update_weekly_payroll.pl' method='post'>
  <input type="hidden" name="supNum" value='$num'/>
  <input type="hidden" name="supName" value='$name'/>
  <input type="hidden" name="supJob" value='$job_id'/>
  <input type="hidden" name="supLoc" value='$loc'/>
  <input type="hidden" name="weekNum" value='$activity'/>
  <input type="hidden" name="payNum" value='$payNum'/>
  <input type="hidden" name="start" value='$start'/>
  <input type="hidden" name="end" value='$end'/>
  <table>
    <tr>
      <th>Emp# &nbsp&nbsp&nbsp</th>
      <th>Name &nbsp&nbsp&nbsp</th>
      <th>Total &nbsp&nbsp&nbsp</th>
      <th>&nbsp&nbsp</th>
      <th>Reg &nbsp&nbsp&nbsp</th>
      <th>1.5x &nbsp&nbsp&nbsp</th>
      <th>2.0x &nbsp&nbsp&nbsp</th>
      <th>Vac &nbsp&nbsp&nbsp</th>
      <th>Stat &nbsp&nbsp&nbsp</th>
      <th>Sick &nbsp&nbsp&nbsp</th>
      <th>Brv &nbsp&nbsp&nbsp</th>
      <th>Abs &nbsp&nbsp&nbsp</th>
      <th>Loa &nbsp&nbsp&nbsp</th>
    </tr>
ENDHEADER
 for my $i (0..$#pay ) {
   my $num = $pay[$i][0];
   my $name = $pay[$i][1];
	my $hours = $pay[$i][2];
	my $reg = $pay[$i][3];
	my $ot1 = $pay[$i][4];
	my $ot2 = $pay[$i][5];
	my $vac = $pay[$i][6];
	my $sta = $pay[$i][7];
	my $sic = $pay[$i][8];
	my $brv = $pay[$i][9];
	my $abs = $pay[$i][10];
	my $loa = $pay[$i][11];
#   print "THE DATE IS: $date for $name NUM: $num STATUS: $status<br/>";

   print <<ENDHEADER;
   <tr>
     <td>&nbsp&nbsp&nbsp $num</td>
     <td><a href="trak_03_sup_weekly_employee_hours.pl?num=$num&name=$name&week=$activity&total=$hours&date_str=$week_str"  target="_blank">&nbsp&nbsp&nbsp $name</td>
     <td>&nbsp&nbsp&nbsp $hours</td>
     <td>&nbsp&nbsp</td>
     <td>$reg</td>
     <td>$ot1</td>
     <td>$ot2</td>
     <td>$vac</td>
     <td>$sta</td>
     <td>$sic</td>
     <td>$brv</td>
     <td>$abs</td>
     <td>$loa</td>
   </tr>
ENDHEADER
 }
 print <<ENDHEADER;
 </table>
 <br/><br/>
 <p><a href="${cgi_ref}trak_00_sup_activity.pl?num=$num&name=$name&job=$job&loc=$loc">Return to Activities</a></p>
 <br/><br/>
 </form>
 </body>
 </html>
ENDHEADER
}


